package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorHeadline;
import guoanmaker.operator.business.service.OperatorHeadlineServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorHeadlineController
 * @Description: OperatorHeadlineController 
 * @date 2017/7/09 上午10:11 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_HEADLINE)
public class OperatorHeadlineController {
    private final AjaxJson ajaxJson;
    private final OperatorHeadlineServiceInterface operatorHeadlineServiceInterface;

    public OperatorHeadlineController(AjaxJson ajaxJson, OperatorHeadlineServiceInterface operatorHeadlineServiceInterface) {
        this.ajaxJson = ajaxJson;
        this.operatorHeadlineServiceInterface = operatorHeadlineServiceInterface;
    }

    /** 
    * @Title: saveHeadline
    * @Description: 新增/修改创客头条 
    * @param: OperatorHeadline   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存创客头条", notes = "新增/修改创客头条")
    @PostMapping(value = "saveHeadline")
    public AjaxJson saveHeadline(@RequestBody InRoundPhone inRoundPhone) {
    	try{
    	OperatorHeadline[] appheads=inRoundPhone.getOperatorHeadlines();
	    	for(OperatorHeadline apphead:appheads){
	    		if(apphead.getId()!=null&&!apphead.getId().equals("")){
	    		operatorHeadlineServiceInterface.deleteHeadlineById(apphead.getId());}
	    		apphead.setCreateDate(new Date());
	    		apphead.setCityId(inRoundPhone.getCityId());
	    		
	    		operatorHeadlineServiceInterface.saveHeadline(apphead);
	    	}
	    	ajaxJson.setSuccess(true).setStatus(200).setData(null);
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /** 
     * @Title: deleteHeadlineById
     * @Description: 根据Id删除创客头条
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除创客头条", notes = "根据Id删除创客头条")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteHeadlineById(@PathVariable(value = "id") String id) {
        operatorHeadlineServiceInterface.deleteHeadlineById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorHeadline    
     * @Title: getHeadlineById 
     * @Description: 根据Id查询创客头条
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询创客头条", notes = "根据Id查询创客头条")
    @GetMapping(value = "getHeadlineById")
    public AjaxJson getHeadlineById(@RequestParam(value = "id") String id) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHeadlineServiceInterface.getHeadlineById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHeadline
     * @Description: 查找所有创客头条 
     * @throws 
     */
    @ApiOperation(value = "根据code查找所有创客头条", notes = "根据code查找所有创客头条")
    @PostMapping(value = "getAllHeadline")
    public AjaxJson getAllHeadline(String cityId) {
    	List<OperatorHeadline> heads=operatorHeadlineServiceInterface.getAllHeadline(cityId);
    	if(heads.size()!=0){
    		ajaxJson.setSuccess(true).setStatus(200).setData(heads);
    	}else{
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHeadlineByPage 
     * @Description: 查询所有创客头条并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询创客头条", notes = "分页查询创客头条")
    @GetMapping(value = "getAllHeadlineByPage")
    public AjaxJson getAllHeadlineByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHeadlineServiceInterface.getAllHeadlineByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getHeadlineByPageOrderByWeight 
     * @Description: 按权重排序查询创客头条并分页 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "按权重排序查询创客头条并分页", notes = "按权重排序查询创客头条并分页")
    @GetMapping(value = "getHeadlineOrderByWeight")
    public AjaxJson getHeadlineByPageOrderByWeight(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHeadlineServiceInterface.getHeadlineByPageOrderByWeight(page, size));
    }

}
